null
null
,通常用來明確地表示「沒有值」或「空物件」的狀態。null
的型別是物件。null
的型別是物件。null
。let obj = null;
console.log(typeof obj); // "object"
undefined
undefined
。undefined
。undefined
。在鬆散的比較中, null
== undefined
,但在嚴格比較中 === 它們是不相等的。
console.log(null == undefined); // true
console.log(null === undefined); // false
let x;
console.log(x); // undefined
console.log(typeof x); // "undefined"
"use strict";
x = 1; //會在嚴格模式下拋出 ReferenceError 錯誤,因為變數沒有被宣告
console.log(x);
在非嚴格模式下,這種行為會自動在全域範圍創建變數,這可能導致意想不到的副作用。
x = 1; // 非嚴格模式下會隱式創建全域變數 x
console.log(x); // 1
Type Utilities
In this question, we will implement the following utility functions to determine the types of primitive values.
export function isBoolean(value) {
return typeof value === "boolean";
}
export function isNumber(value) {
return typeof value === "number";
}
export function isNull(value) {
return value === null;
}
export function isString(value) {
return typeof value === "string";
}
export function isSymbol(value) {
return typeof value === "symbol";
}
export function isUndefined(value) {
return typeof value === "undefined";
}
// This is a JavaScript Quiz from BFE.dev
console.log(JSON.stringify([1, 2, null, 3]));
console.log(JSON.stringify([1, 2, undefined, 3]));
console.log(null === undefined);
console.log(null == undefined);
console.log(null == 0);
console.log(null < 0);
console.log(null > 0);
console.log(null <= 0);
console.log(null >= 0);
console.log(undefined == 0);
console.log(undefined < 0);
console.log(undefined > 0);
console.log(undefined <= 0);
console.log(undefined >= 0);
// This is a JavaScript Quiz from BFE.dev
console.log(JSON.stringify([1, 2, null, 3])); //"[1,2,null,3]"
console.log(JSON.stringify([1, 2, undefined, 3])); //"[1,2,null,3]",undefined 被轉換為 null,因為 JSON 不支持 undefined 作為有效值
console.log(null === undefined); //false
console.log(null == undefined); //true
console.log(null == 0); //false
console.log(null < 0); //false
console.log(null > 0); //false
console.log(null <= 0); //true,null 會被轉換為 0
console.log(null >= 0); //true,null 會被轉換為 0
console.log(undefined == 0); //false,undefined 會被轉換為 NaN
console.log(undefined < 0); //false,undefined 會被轉換為 NaN
console.log(undefined > 0); //false,undefined 會被轉換為 NaN
console.log(undefined <= 0); //false,undefined 會被轉換為 NaN
console.log(undefined >= 0); //false,undefined 會被轉換為 NaN